package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.bean.CodeInfoBean;
import cc.rengu.igas.momp.facade.bean.RuleInfoBean;
import cc.rengu.igas.momp.facade.request.QueryCashierCashbackBonusRuleRequest;
import cc.rengu.igas.momp.facade.response.QueryCashierCashbackBonusRuleResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 收银员返现规则查询
 *
 * @author Jinan Liu
 * @date 2020/4/2 17:49
 * @description
 */
public class QueryCashierCashbackBonusRuleService extends RadpService {
    private String globalInstId;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryCashierCashbackBonusRuleRequest queryCashierCashbackBonusRuleRequest = new QueryCashierCashbackBonusRuleRequest();
            ConvertUtil.convertOutput(queryCashierCashbackBonusRuleRequest);

            QueryCashierCashbackBonusRuleResponse queryCashierCashbackBonusRuleResponse = new QueryCashierCashbackBonusRuleResponse();
            queryCashierCashbackBonusRuleResponse.setHeader(queryCashierCashbackBonusRuleRequest.getHeader());
            BizResponse<QueryCashierCashbackBonusRuleResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryCashierCashbackBonusRuleResponse);

            String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            globalInstId = queryCashierCashbackBonusRuleRequest.getHeader().getInstId();
//            rglog.info("机构<{}>在<{}>由<{}>开始查询代理商分润类营销活动详细信息流程", globalInstId, timeStamp);

            /* 检查操作员机构标示 */
            if (StringUtil.isNullorEmpty(globalInstId)) {
                rglog.error("法人机构号<{}>为空!", globalInstId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }


            // 服务调用
            BizResponse<QueryCashierCashbackBonusRuleResponse> bizResponseNew = queryCashierCashbackBonusRule(xmlTreeUtil, queryCashierCashbackBonusRuleRequest, queryCashierCashbackBonusRuleResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 收银员返现积分规则查询
     *
     * @param xmlTreeUtil                           内部XML树
     * @param queryCashierCashbackBonusRuleRequest  收银员返现积分规则查询接口请求报文
     * @param queryCashierCashbackBonusRuleResponse 收银员返现积分规则查询接口应答报文
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryCashierCashbackBonusRuleResponse>收银员返现积分规则查询接口应答报文
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 14:50
     */

    private BizResponse<QueryCashierCashbackBonusRuleResponse> queryCashierCashbackBonusRule(XmlTreeUtil xmlTreeUtil, QueryCashierCashbackBonusRuleRequest queryCashierCashbackBonusRuleRequest, QueryCashierCashbackBonusRuleResponse queryCashierCashbackBonusRuleResponse) throws Exception {

        BizResponse<QueryCashierCashbackBonusRuleResponse> bizResponse = new BizResponse<>();
        /* 先查询缓存 */
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(globalInstId, "MC_BONUS_POINT_CASHBACK_RULE", "RULE_INFO");
        if (sysParam == null) {
            rglog.error("根据法人机构号<{}>查询系统参数表中的积分规则信息失败!", globalInstId);
            throw new BizException(RespCodeEnum.GET_SYS_PARAM_ERROR.getRespCode(), RespCodeEnum.GET_SYS_PARAM_ERROR.getRespDesc());
        }
        //从系统参数表取到的json字符串解析
        String rule = sysParam.getParamValue();
        JSONObject jsonObjects = JSON.parseObject(rule);
        //收银员返现积分规则信息List
        List<RuleInfoBean> ruleInfoBeanList = new ArrayList<>();
        JSONArray jsonArray = JSONArray.parseArray(jsonObjects.get("rule").toString());
        for (int l = 0; l < jsonArray.size(); l++) {
            RuleInfoBean ruleInfoBean = new RuleInfoBean();
            JSONObject jsonObject = jsonArray.getJSONObject(l);
            ruleInfoBean.setRuleIndex(jsonObject.getString("index"));
            ruleInfoBean.setDisplayType(jsonObject.getString("code_type"));
            JSONArray codevalueAry = jsonObject.getJSONArray("code_value");
            List<CodeInfoBean> codeInfoBeanList = new ArrayList<>();
            for (int j = 0; j < codevalueAry.size(); j++) {
                JSONObject codeValeObj = codevalueAry.getJSONObject(j);
                for (Map.Entry<String, Object> entry : codeValeObj.entrySet()) {
                    CodeInfoBean codeInfoBean = new CodeInfoBean();
                    codeInfoBean.setCode(entry.getKey());
                    codeInfoBean.setCodeValue((String) entry.getValue());
                    codeInfoBeanList.add(codeInfoBean);
                }
            }
            ruleInfoBean.setCodeInfoList(codeInfoBeanList);
            ruleInfoBeanList.add(ruleInfoBean);
        }
        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        queryCashierCashbackBonusRuleResponse.setRuleInfoList(ruleInfoBeanList);
        queryCashierCashbackBonusRuleResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryCashierCashbackBonusRuleResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryCashierCashbackBonusRuleResponse);
        return bizResponse;
    }
}
